package com.csv;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.sql.Date;
import java.text.SimpleDateFormat;

import com.db.processror.TradeProcessor;
import com.trade.TradeBean;

public class CSVReader {
	public void loadData() throws Exception{
		BufferedReader br = new BufferedReader(new FileReader("D:\\Study\\java\\TradeReconcile\\Trader\\vsf-fy07.csv"));
		String line="";
		line=br.readLine();
		while((line=br.readLine())!=null){
			TradeBean trade = processLineToTrade(line);
			TradeProcessor.insertTrade(trade);			
		}
		TradeProcessor.closeCon();
	}
	
	public void loadData(File f) throws Exception{
		BufferedReader br = new BufferedReader(new FileReader(f));
		String line="";
		line=br.readLine();
		while((line=br.readLine())!=null){
			TradeBean trade = processLineToTrade(line);
			TradeProcessor.insertTrade(trade);			
		}
		TradeProcessor.closeCon();
	}
	
	public static TradeBean processLineToTrade(String line) throws Exception{
		TradeBean trade = new TradeBean();
		String[] values = line.split(",");
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy");
		java.sql.Date d = new Date(sdf.parse(values[0]).getTime());
		trade.setDate(d);
		trade.setStock(values[1]);
		trade.setAction(values[2]);
		trade.setQty(Integer.parseInt(values[3]));
		trade.setPrice(Double.parseDouble(values[4]));
		trade.setTradeValue(Double.parseDouble(values[5]));
		trade.setBrokerageincltaxes(Double.parseDouble(values[6]));
		trade.setOrderRef(values[7]);
		trade.setSettlement(values[8]);
		trade.setSegment(values[9]);
		trade.setDPIdClientDPId(values[10]);
		trade.setExchange(values[11]);
		return trade;
	}
}
